<template>
  <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
          <h3>年级:</h3> 
          <div style="width: 10px;"></div>
          <el-select v-model="formInline.gradeLevel" style="width: 200px" clearable placeholder="请选择年级">
              <el-option v-for="item in LevelStore.list" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
      </el-form-item>
      <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
  </el-form>

  <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" />
      <el-table-column prop="title" label="标题" width="100" />
      <el-table-column prop="gradeLevel" label="学级">
          <template #default="scope">
              {{ LevelStore.list.find(item => item.value === scope.row.gradeLevel)?.name || scope.row.gradeLevel }}
          </template>
      </el-table-column>
      <el-table-column prop="createUserName" label="发送人" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column fixed="right" label="操作" width="">
          <template #default="scope">
              <el-button @click="Edit(scope.row)">编辑</el-button>
              <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
      </el-table-column>
  </el-table>
  
  <div style="margin-top: 20px; display: flex; justify-content: center;">
      <el-pagination 
          background 
          layout="prev, pager, next" 
          :total="total" 
          :page-size="formInline.pageSize"
          :current-page="formInline.pageIndex"
          @current-change="handleCurrentChange" />
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useLevelStore } from '@/stores/level'
import { taskPage, taskDelete, type taskType } from '@/api/renwu'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

const LevelStore = useLevelStore()
const router = useRouter()

// 使用与接口匹配的参数格式
const formInline = reactive({
  gradeLevel: null as number | null,
  pageIndex: 1,
  pageSize: 10
})

const tableData = ref<taskType[]>([])
const total = ref(0)

// 获取任务列表
const getList = () => {
  taskPage({
      gradeLevel: formInline.gradeLevel,
      pageIndex: formInline.pageIndex,
      pageSize: formInline.pageSize
  }).then(res => {
      if (res.code === 1 && res.response) {
          tableData.value = res.response.list
          total.value = res.response.total
      } else {
          ElMessage.error(res.message || '获取数据失败')
      }
  }).catch(error => {
      ElMessage.error('请求失败: ' + error.message)
  })
}

// 初始化加载列表
getList()

const onSubmit = () => {
  formInline.pageIndex = 1
  getList()
}

// 分页处理
const handleSizeChange = (val: number) => {
  formInline.pageSize = val
  getList()
}

const handleCurrentChange = (val: number) => {
  formInline.pageIndex = val
  getList()
}

// 编辑功能 - 跳转到创建页面并携带ID
function Edit(row: taskType) {
  router.push(`/renwucreate?taskId=${row.id}`)
}

// 删除功能
const handleDelete = (id: number) => {
  ElMessageBox.confirm(
      '确定要删除此任务吗？此操作无法撤销。',
      '警告',
      {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }
  ).then(() => {
      // 用户确认删除
      taskDelete(id).then(res => {
          if (res.code === 1) {
              ElMessage.success('删除成功')
              getList() // 刷新列表
          } else {
              ElMessage.error(res.message || '删除失败')
          }
      }).catch(error => {
          console.error('删除任务时出错:', error)
          ElMessage.error('删除失败，请稍后重试')
      })
  }).catch(() => {
      // 用户取消删除
      ElMessage.info('已取消删除')
  })
}
</script>